package com.vcc.msc.sbs.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.vcc.msc.sbs.R;

public class ImageAdapter extends BaseAdapter {

	int mGalleryItemBackground;
	private Context mContext;
	private List<Bitmap> imgs;

	public ImageAdapter(Context c, List<byte[]> list) {
		mContext = c;
		this.imgs = getBitMapList(list);
		TypedArray attr = mContext
				.obtainStyledAttributes(R.styleable.wesGallery);
		mGalleryItemBackground = attr.getResourceId(
				R.styleable.wesGallery_android_galleryItemBackground, 0);
		attr.recycle();
	}

	private List<Bitmap> getBitMapList(List<byte[]> list) {
		if (list != null && list.size() != 0) {
			imgs = new ArrayList<Bitmap>();
			for (byte[] imgByte : list) {
				Bitmap img = BitmapFactory.decodeByteArray(imgByte, 0,
						imgByte.length);
				imgs.add(img);
			}
		} else {
			// TODO Remove Mock
			imgs = new ArrayList<Bitmap>();
			Bitmap mockImg1 = BitmapFactory.decodeResource(
					mContext.getResources(), R.drawable.bild1);
			Bitmap mockImg2 = BitmapFactory.decodeResource(
					mContext.getResources(), R.drawable.bild2);
			imgs.add(mockImg2);
			imgs.add(mockImg1);

		}
		return imgs;
	}

	@Override
	public int getCount() {
		if (imgs != null)
			return imgs.size();
		else
			return 0;
	}

	@Override
	public Object getItem(int position) {
		return imgs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView = new ImageView(mContext);

		imageView.setImageBitmap((Bitmap) getItem(position));
		imageView.setLayoutParams(new Gallery.LayoutParams(300, 300));
		imageView.setPadding(20, 20, 20, 20);
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setBackgroundResource(mGalleryItemBackground);

		return imageView;
	}

}
